/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.skill.executor;

import com.lineage.server.PacketCreate;
import com.lineage.server.datatables.NpcTable;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.Instance.L1SummonInstance;
import com.lineage.server.model.skill.L1SkillExecutor;
import com.lineage.server.packetserver.S_ShowSummonList;
import com.lineage.server.templates.L1Npc;

/**
 * Class <code>SummonMonster</code> 法师魔法:召唤术(30/30/3).
 * 召唤兽种类：
 * 
 * 限制等级：防御型、攻击型、远距离型(魔法型)
 * 
 * 28级：哈柏哥布林、艾多伦(召戒)、安普(召戒)
 * 32级：甘地妖魔、都达玛拉妖魔(召戒)、妖魔巡守(召戒)
 * 36级：狂野毒牙、狂野之毒(召戒)、狂野之魔(召戒)
 * 40级：食人妖精、食人妖精王(召戒)、冰人(召戒)
 * 44级：狂暴蜥蜴人、重装蜥蜴人(召戒)、高等蜥蜴人(召戒)
 * 48级：火蜥蜴、火焰战士(召戒)、火焰弓箭手(召戒)
 * 52级：魔熊、魔狼(召戒)、魔蝙蝠(召戒)
 * 56级：巨大守护蚂蚁(召戒)、强化白蚂蚁群(召戒)、巨大强化白蚂蚁(召戒)
 * 60级：地狱奴隶(召戒)、闇精灵王(召戒)、食腐兽(召戒)
 * 64级+魅力14：地狱束缚犬(召戒)
 * 68级+魅力36：变形怪首领(召戒)
 * 72级+魅力36：巨大牛人(召戒)
 * 72级+魅力44：黑豹(召戒)
 * 
 * 召唤兽数量：
 * 
 * 52级以下：(魅力 + 6) / 8
 * 56级：(魅力 + 6) / 10
 * 60级：(魅力 + 6) / 12
 * 64级、魅力14：(魅力 + 6) / 20
 * 68级、魅力36：1只
 * 72级、魅力36：1只
 * 72级、魅力44：1只
 * 
 * 每次使用增加10点邪恶值
 * 
 * @author jrwz
 * @version 2012-7-24下午08:04:15
 * @see com.lineage.server.model.skill.executor
 * @since JDK1.7
 */
public final class SummonMonster extends L1SkillExecutor {

    /**
     * 取得实例.
     * 
     * @return 实例
     */
    public static L1SkillExecutor get() {
        return new SummonMonster();
    }

    /** 法师魔法:召唤术(30/30/3). */
    private SummonMonster() {
    }

    @Override
    public void execute(final L1Character user, final L1Character cha,
            final L1Character target, final int targetID, final int skillId,
            final int getBuffIconDuration) {
        if (user instanceof L1PcInstance) {
            final L1PcInstance pc = (L1PcInstance) user;
            final int level = pc.getLevel();
            // int[] summons;
            if (pc.getMap().isRecallPets()) {
                if (pc.getInventory().checkEquipped(20284)) {
                    pc.sendPackets(new S_ShowSummonList(pc.getId()));
                    if (!pc.isSummonMonster()) {
                        pc.setSummonMonster(true);
                    }
                } else {
                    /*
                     * summons = new int[] { 81083, 81084, 81085, 81086,
                     * 81087, 81088, 81089 };
                     */
                    final int[] summons = { 81210, 81213, 81216, 81219, 81222,
                            81225, 81228 };
                    int summonid = 0;
                    // int summoncost = 6;
                    final int summoncost = 8;
                    int levelRange = 32;
                    for (int i = 0; i < summons.length; i++) { // 该当ＬＶ范围检索
                        if ((level < levelRange) || (i == summons.length - 1)) {
                            summonid = summons[i];
                            break;
                        }
                        levelRange += 4;
                    }

                    int petcost = 0;
                    final Object[] petlist = pc.getPetList().values().toArray();
                    for (final Object pet : petlist) {
                        petcost += ((L1NpcInstance) pet).getPetcost();
                    }
                    int pcCha = pc.getCha();
                    if (pcCha > 34) { // max count = 5
                        pcCha = 34;
                    }
                    final int charisma = pcCha + 6 - petcost;
                    // int charisma = pc.getCha() + 6 - petcost;
                    final int summoncount = charisma / summoncost;
                    final L1Npc npcTemp = NpcTable.getInstance().getTemplate(
                            summonid);
                    for (int i = 0; i < summoncount; i++) {
                        final L1SummonInstance summon = new L1SummonInstance(
                                npcTemp, pc);
                        summon.setPetcost(summoncost);
                    }
                }
            } else {
                PacketCreate.get().sendSinglePackets(pc, "没有任何事情发生");
            }
        }
    }

    @Override
    public void stop(final L1Character cha, final int skillId) {
        // TODO Auto-generated method stub
    }
}
